# *---encoding:UTF-8--*

'''
评论区 获取粉丝profile_url通过粉丝profile_url 获取粉丝 profile_url 以此递归
'''

import requests,random,time
from fake_useragent import UserAgent
from config import *
from mongo import MongoClient


class Weibo( object ):

    def __init__(self):

        self.ua = UserAgent()
        self.mongo = MongoClient()

    # 通过评价获取评价粉丝
    def get_follow_fans(self):

        r = requests.get( url = domain["start_url"] + "mid=4425068320667318&max_id_type=0",headers=header )

        if r.status_code == 200:

            items = r.json()
            # 清洗关注者数据
            self.parse_follower_center( items )

            max_id = items["data"]["max_id"]

            # 递归调用
            self.next_follow_fans( max_id )

            return

    def next_follow_fans(self,max_id = None):

        r = requests.get(url=domain["start_url"] + "mid=4425068320667318&&max_id={0}&max_id_type=0".format( str(max_id) ), headers=header)

        # 延迟5-10
        time.sleep( random.randint(5,10))

        if r.status_code == 200:

            items = r.json()
            # 清洗关注者数据
            self.parse_follower_center(items)

            max_id = items["data"]["max_id"]
            # 递归调用
            self.next_follow_fans(max_id)

            return  None

    #更新用户详情
    def upper_user_info(self,user_id):

        data = {
            "uid":user_id,
            "value":user_id
        }
        # https://m.weibo.cn/api/container/getIndex?containerid=2302833764878814_-_INFO&title=%E5%9F%BA%E6%9C%AC%E8%B5%84%E6%96%99&luicode=10000011&lfid=2302833764878814
        r = requests.get( url = domain["user_url"] + "uid={uid}&type=uid&value={value}".format( **data ),headers=header )

        if r.status_code == 200:

            #获取container_id
            container_id = r.json()["data"]["tabsInfo"]["tabs"][0]["containerid"]

            data["containerid"] = str( container_id )

            #获取更多资料页面
            rep = requests.get( url = domain["user_url"] + "uid={uid}&type=uid&value={value}&containerid={containerid}".format(**data),headers=header )

            if rep.status_code == 200:

                try:

                    #获取更多资料地址并请求
                    info_url = rep.json()["data"]["cards"][2]["card_group"][2]["scheme"].replace("https://m.weibo.cn/p/index?",domain["user_url"])

                    r = requests.get( url = info_url,headers=header )

                    if r.status_code == 200:

                        data = self.parse_user_center( r.json( ))

                        return data

                except Exception as e:

                    return {"address":""}

    #处理数据
    def parse_follower_center(self,items):

        for item in items["data"]["data"]:

            data = {
                # 评论内容
                "content": item["text"],
                # 用户id
                "user_id": item["user"]["id"],
                # 昵称
                "nickname": item["user"]["screen_name"],
                # 性别
                "gender": item["user"]["gender"],
                # 粉丝数量
                "followers_count": item["user"]["followers_count"] if "followers_count" in item["user"] else "",
                # 头像地址
                "avatar_url": item["user"]["profile_image_url"],
                # 主页地址
                "main_url": item["user"]["profile_url"],
                # 认证介绍
                "verified_reason": item["user"]["verified_reason"] if "verified_reason" in item["user"] else "",
                # 描述信息
                "description": item["user"]["description"],
                # 评论喜欢
                "like_count": item["like_count"],
            }

            # 延迟5-10
            time.sleep(random.randint(5, 10))

            data.update( self.upper_user_info( data["user_id"] ))

            #存储mongo数据库
            self.mongo.saveMongo( data )


    #处理用户信息
    def parse_user_center(self,items=None):

        items = {'ok': 1, 'data': {'cards': [{'card_type': 11, 'card_group': [{'card_type': 42, 'display_arrow': 0, 'desc': '账号信息'}, {'card_type': 41, 'item_name': '昵称', 'item_content': '晓雪'}, {'item_type': 'verify_yellow', 'display_arrow': 1, 'actionlog': {'act_code': '594', 'ext': 'uid:3839575640|ouid:1198253935|verified_type:0|ptype:0|load_read_level:0', 'oid': '2302831198253935_-_INFO', 'fid': '2302831198253935_-_INFO', 'cardid': '230283_-_WEIBO_INDEX_PROFILE_VERFIED_CARD', 'uicode': '10000198'}, 'card_type': 41, 'item_content': '《ELLE》主编', 'scheme': 'http://verified.weibo.com/verify?luicode=10000011&lfid=2302831198253935_-_INFO', 'itemid': '2302831198253935_-_WEIBO_INDEX_PROFILE_VERFIED_CARD'}, {'card_type': 41, 'item_name': '简介', 'item_content': '乐妈晓雪'}, {'card_type': 41, 'item_name': '注册时间', 'item_content': '2009-08-28'}, {'item_name': '阳光信用', 'display_arrow': 1, 'card_type': 41, 'item_content': '信用极好', 'actionlog': {'act_code': '594', 'ext': 'uid:3839575640|ouid:1198253935|verified_type:0|ptype:0|load_read_level:0', 'oid': 'https://service.account.weibo.com/sunshine/guize?sinainternalbrowser=topnav', 'fid': '2302831198253935_-_INFO', 'cardid': '230283_-_WEIBO_INDEX_USERINFO_CREDIT', 'uicode': '10000198'}, 'scheme': 'https://service.account.weibo.com/sunshine/guize?sinainternalbrowser=topnav&luicode=10000011&lfid=2302831198253935_-_INFO'}]}, {'card_type': 11, 'card_group': [{'card_type': 42, 'display_arrow': 0, 'desc': '个人信息'}, {'card_type': 41, 'item_name': '性别', 'item_content': '女'}, {'card_type': 41, 'item_name': '所在地', 'item_content': '北京 朝阳区'}]}, {'card_type': 11, 'is_asyn': 1, 'card_group': [], 'itemid': '2306181198253935_-_3861', 'async_api': '/api/container/getItem?itemid=2306181198253935_-_3861&download='}, {'card_type': 11, 'is_asyn': 1, 'card_group': [], 'itemid': '2306181198253935_-_profileinterest', 'async_api': '/api/container/getItem?itemid=2306181198253935_-_profileinterest&download='}, {'display_arrow': 1, 'is_asyn': 0, 'card_id': 'renling', 'show_type': 0, 'buttontitle': '', 'title': '', 'card_group': [{'actionlog': {'act_code': 594, 'ext': 'uid:3839575640;ouid:1198253935;verified_type:0;ptype:0;load_read_level:0', 'oid': '23085800241198253935', 'fid': '2302831198253935_-_INFO', 'cardid': '230283_-_WEIBO_INDEX_PROFILE_LBSZUJI', 'uicode': '10000198'}, 'pic': '', 'display_arrow': 1, 'title_extra_text': '全部', 'card_type': 42, 'scheme': 'https://m.weibo.cn/p/index?containerid=2310340013&count=5&extparam=uid%2C1198253935&needlocation=1&title=%E7%AD%BE%E5%88%B0%E8%B6%B3%E8%BF%B9&luicode=10000011&lfid=2302831198253935_-_INFO', 'desc': '签到足迹', 'display_type': 0}, {'display_arrow': 1, 'card_display_type': 1, 'desc2_struct': '签到 6次 | 7-18 来过 纽约', 'right_pannel': [], 'buttons': [], 'card_id': 'renling4395501883198615', 'desc1': '纽约总部开会。今天最爱这句：Teamwork makes the dream work 💪#雪职场分享# 真的，没有团队彼此信任、没有小伙伴彼此支撑、再强大再有才，不过只能是个自大的leader,无法完成dream work.[心]  \u200b', 'title': '', 'desc2': '签到 6次 | 7-18 来过 纽约', 'card_type_name': '', 'card_type': 8, 'scheme': 'https://m.weibo.cn/p/index?containerid=2310340006&count=10&needlocation=1&title=%E7%BE%8E%E5%9B%BD&extparam=country%2C80001%2C1198253935&luicode=10000011&lfid=2302831198253935_-_INFO', 'pic': 'https://ww2.sinaimg.cn/wap180/4e704b16jw1eswpv5urh8j206w06w74m.jpg', 'title_sub': '美国'}, {'display_arrow': 1, 'card_display_type': 1, 'desc2_struct': '签到 22次 | 11-23 来过 牛街聚宝源涮肉望京店', 'right_pannel': [], 'buttons': [], 'card_id': 'renling4441925384373328', 'desc1': '天气预报说大北京明天降温，今天赶紧给自己补点热量[挤眼]没那么情迷火锅，但喜欢火锅店的气氛，热气腾腾地烟火气旺盛，吃着吃着就鼻涕眼泪热泪盈眶了[太开心]涮羊肉还好，但刚出炉焦焦嫩嫩小火烧，欲罢不能[憧憬]2元一个，吃了4个，打包14个[哈哈]下周请叫我“火烧姐”[笑cry]  \u200b', 'title': '', 'desc2': '签到 22次 | 11-23 来过 牛街聚宝源涮肉望京店', 'card_type_name': '', 'card_type': 8, 'scheme': 'https://m.weibo.cn/p/index?containerid=2310340006&count=10&needlocation=1&title=%E5%8C%97%E4%BA%AC&extparam=city%2C0010%2C1198253935&luicode=10000011&lfid=2302831198253935_-_INFO', 'pic': 'https://ww1.sinaimg.cn/wap180/82ef82cbjw1fb57k4d294j2046046t8x.jpg', 'title_sub': '北京'}, {'display_arrow': 1, 'card_display_type': 1, 'desc2_struct': '签到 46次 | 8-26 来过 上海K11购物艺术中心', 'right_pannel': [], 'buttons': [], 'card_id': 'renling4409680061638173', 'desc1': '加班，去大厦停车场透透气。又见旁边工地的大吊车挑灯夜战......爱上了从这里看夜上海，每盏灯都罩着一个忙碌的身影吧，以为大家都睡了，其实比自己更用功更辛苦更奔波的人，比比皆是，就如眼前这一片的街灯、路灯、车灯，交相辉映[月亮]  \u200b', 'title': '', 'desc2': '签到 46次 | 8-26 来过 上海K11购物艺术中心', 'card_type_name': '', 'card_type': 8, 'scheme': 'https://m.weibo.cn/p/index?containerid=2310340006&count=10&needlocation=1&title=%E4%B8%8A%E6%B5%B7&extparam=city%2C0021%2C1198253935&luicode=10000011&lfid=2302831198253935_-_INFO', 'pic': 'https://ww3.sinaimg.cn/wap180/4e704b16jw1esr3838rytj207h07hwf6.jpg', 'title_sub': '上海'}], 'scheme': '', 'card_type': 11, 'card_type_name': ''}, {'card_type': 11, 'is_asyn': 1, 'card_group': [], 'itemid': '2306181198253935_-_likes', 'async_api': '/api/container/getItem?itemid=2306181198253935_-_likes&download='}], 'cardlistInfo': {'show_style': 1, 'button': None, 'can_shared': 0, 'cardlist_menus': [{'name': '刷新', 'type': 'button_menus_refresh'}, {'name': '返回首页', 'params': {'scheme': 'sinaweibo://gotohome'}, 'type': 'gohome'}], 'v_p': '42', 'cardlist_title': '', 'desc': '', 'containerid': '2302831198253935_-_INFO', 'page': None}, 'scheme': 'sinaweibo://cardlist?containerid=2302831198253935_-_INFO&extparam=&title=%E5%9F%BA%E6%9C%AC%E8%B5%84%E6%96%99&luicode=10000011&lfid=2302831198253935&oid=4445531080013115&v_p=42&fid=2302831198253935_-_INFO&uicode=10000011'}}

        address = items["data"]["cards"][1]["card_group"][2]["item_content"] if "item_content" in items["data"]["cards"][1]["card_group"][2] else ''

        data = {

            "address":address
        }

        return data

if __name__ == '__main__':

    w = Weibo()
    w.get_follow_fans()

    # w.test()









